package com.xsy.carRental.controller;


import com.xsy.carRental.constant.MessageConstant;
import com.xsy.carRental.dto.CustomerAddDTO;
import com.xsy.carRental.dto.CustomerPageQueryDTO;
import com.xsy.carRental.dto.CustomerSearchQueryDTO;
import com.xsy.carRental.result.PageResult;
import com.xsy.carRental.result.Result;
import com.xsy.carRental.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/customer")
@Slf4j

public class CustomerController {

    @Autowired
    private CustomerService customerService;


    /**
     * 用户注册
     *
     * @param customerAddDTO
     * @return
     */
    @PostMapping
    public Result addCustomer(@RequestBody CustomerAddDTO customerAddDTO) {
        log.info("新用户注册{}", customerAddDTO);
        customerService.add(customerAddDTO);

        return Result.success(MessageConstant.CUSTOMERADD_SUCCESS);
    }

    /**
     * 分页查询用户
     *
     * @param customerPageQueryDTO
     * @return
     */
    @GetMapping
    public Result<PageResult> pageQuery(CustomerPageQueryDTO customerPageQueryDTO) {
        log.info("分页查询用户");
        PageResult pageResult = customerService.pageQuery(customerPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据id删除用户
     *
     * @param id
     */
    @DeleteMapping("/{id}")
    public Result DeleteById(@PathVariable int id) {
        log.info("根据id{}删除用户", id);
        customerService.deleteById(id);

        return Result.success();
    }

    /**
     * 根据三个可选条件查询用户
     *
     * @param searchQueryDTO
     * @return
     */
    @GetMapping("/search")
    public Result<PageResult> SearchQuery(CustomerSearchQueryDTO searchQueryDTO) {

        log.info("根据条件分页查询用户");
        PageResult pageResult = customerService.SearchQuery(searchQueryDTO);

        return Result.success(pageResult);
    }

}
